package basic.tutorial.form;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;


/*
 * Clasa in care este implementat formularul
 * Depinde de : addentry.xml
 * Exemplifica:
 * 		AutoCompleteTextView
 * 		EditText
 * 		SeekBar
 * 		
 */
public class AddForm extends Activity{

	public static ArrayList<String> author_list;
	public static String[] items ;
	
	AutoCompleteTextView authors;
	SeekBar pages;
	Spinner year;
	EditText title;
	int year_pos;
	
	// Functie de initializare a listelor folosite pentru 
	// AutoComplete si pentru Spinner
	private void initLists(){
		 items = new String[] {"2000", "2001", "2002", 
				 "2003", "2004", "2005",
	    		 "2006", "2007", "2008",
	    		 "2009", "2010", "2011"};
		 
		 author_list = new ArrayList<String>();
		 
		 author_list.add("Tudor Arghezi");
	     author_list.add("Mihai Eminescu");
	     author_list.add("Ion Creanga");
	     author_list.add("Mihail Sadoveanu");
	     author_list.add("Mircea Eliade");
		
	}
	
	// Listener pentru Spinner, daca au loc schimbari acestea sunt retinute in
	// variabila year_pos;
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView parent,
	        View view, int pos, long id) {
	      year_pos = pos;
	    }
	    
	    public void onNothingSelected(AdapterView parent) {
	        year_pos = 1;
	      }
	  }
	
	@Override
	  public void onCreate(Bundle savedInstanceState) {
		
	     super.onCreate(savedInstanceState);
	     setContentView(R.layout.addentry);  
	     
	     initLists();
	     
	     // ArrayAdapter este folosit pentru a retine lista de autori 
	     // initializata in functia initLists();
	     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	                android.R.layout.simple_dropdown_item_1line, author_list);
	     authors = (AutoCompleteTextView)
	                findViewById(R.id.author);
	     authors.setAdapter(adapter);
	     
	     // Initializare EditText, spatiu in care utilizatorul poate 
	     // introduce string-ul pe care il doreste
	     title = new EditText(this);
	     title = (EditText)findViewById(R.id.EditText01);
	     
	     // Initializare SeekBar
	     pages = new SeekBar(this);
	     pages.setMax(100);
	     pages = (SeekBar)findViewById(R.id.SeekBar01);
	     
	     // Initializare Spinner 
	     year = (Spinner) findViewById(R.id.Spinner01);
	     
	     // Initializare ArrayAdapter asociat cu spinner si legarea 
	     // acestuia la obiect
	     ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
	                 android.R.layout.simple_spinner_item, items);
	     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	     year.setAdapter(adapter1);
	     year.setOnItemSelectedListener(new MyOnItemSelectedListener());
	   
	     Button save = (Button) findViewById(R.id.Button01);
	     save.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	        	 
	        	 // Obtinerea informatiilor introduse de utilizator
	        	 Item nI = new Item();
	        	 nI.author = authors.getText().toString();
	        	 nI.pageNo = pages.getProgress();
	        	 nI.title = title.getText().toString();
	        	 nI.year = Integer.parseInt( items[year_pos] );
	        	
	        	 // Adaugarea in obectul static
	        	Entry.addEntry(nI);
	        	
	        	// La apasarea butonului Save se intoarce la meniul principal
	        	Intent myIntent = new Intent(AddForm.this, Main.class);
	     		startActivity(myIntent); 
	         }
	     });
	     
	}
}
